package drug

import (
	"Project/global"
	"Project/model/common/response"
	"Project/model/drugs/request"
	resp "Project/model/drugs/response"
	"Project/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type DrugRecordApi struct{}

// AddDrugRecord 新增一条购药记录
func (d *DrugRecordApi) AddDrugRecord(c *gin.Context) {
	// 购药登记信息
	drugRecord := request.DrugRecordRequest{}
	err := c.ShouldBind(&drugRecord)
	global.GVA_LOG.Debug("c.ShouldBind", zap.Any("drugRecord", drugRecord))
	if err != nil {
		global.GVA_LOG.Error("c.ShouldBind err", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 获取当前用户ID
	saleID := utils.GetUserID(c)

	// 存储生成并存储购药记录
	if err = drugRecordService.SaveDrugRecord(saleID, drugRecord); err != nil {
		response.Fail(c)
		return
	}

	// 响应
	response.OkWithMessage("操作成功", c)
}

// DeleteDrugRecord 删除一条购药记录
func (d *DrugRecordApi) DeleteDrugRecord(c *gin.Context) {
	// 获取记录id
	drugRecordIdStr := c.Param("drug_record_id")
	global.GVA_LOG.Debug("传入字段", zap.String("drug_record_id", drugRecordIdStr))
	if drugRecordIdStr == "" {
		global.GVA_LOG.Error("传入字段为空", zap.String("drug_record_id", drugRecordIdStr))
		response.Fail(c)
		return
	}
	// 转为int64
	drugRecordId, err := strconv.ParseInt(drugRecordIdStr, 10, 64)
	if err != nil {
		global.GVA_LOG.Error("传入的字段值不合法", zap.String("drug_record_id", drugRecordIdStr))
		response.Fail(c)
		return
	}
	// 删除记录
	err = drugRecordService.DeleteDrugRecord(drugRecordId)
	if err != nil {
		global.GVA_LOG.Error("删除购药记录表记录失败", zap.Error(err))
		response.Fail(c)
		return
	}
	response.Ok(c)
}

// UpdateDrugRecord 更新购药记录
func (d *DrugRecordApi) UpdateDrugRecord(c *gin.Context) {
	// 获取购药记录ID
	recordID := c.Query("record_id")
	recordId, err := strconv.ParseInt(recordID, 10, 64)
	if err != nil {
		return
	}
	// 获取更新的购药记录信息
	var drugRecord request.DrugRecordRequest
	err = c.ShouldBind(&drugRecord)
	global.GVA_LOG.Debug("c.ShouldBind", zap.Any("drugRecord", drugRecord))
	if err != nil {
		global.GVA_LOG.Error("c.ShouldBind err", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}
	// 更新
	if err = drugRecordService.UpdateDrugRecord(recordId, drugRecord); err != nil {
		global.GVA_LOG.Error("err", zap.Error(err))
		response.Fail(c)
		return
	}
	// 响应
	response.OkWithMessage("更新成功", c)
}

// GetDrugRecord 获取药店购药记录
func (d *DrugRecordApi) GetDrugRecord(c *gin.Context) {

	// 获取查询条件
	var req request.InquireDrugRecordRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		global.GVA_LOG.Error("GetDrugRecord err", zap.Error(err))
		response.FailWithMessage("请求参数错误", c)
		return
	}
	global.GVA_LOG.Debug("GetDrugRecord req", zap.Any("req", req))

	// 获取当前用户ID
	saleID := utils.GetUserID(c)

	// 查询
	recordlist, total, err := drugRecordService.GetAllDrugRecordByShopId(saleID, &req)
	if err != nil {
		global.GVA_LOG.Error(" drugRecordService.GetAllDrugRecord", zap.Error(err))
		response.Fail(c)
		return
	}

	//封装返回
	resp := resp.InquireResponse{
		recordlist,
		total,
	}
	response.OkWithData(resp, c)
}
